package com.xy.config;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//自定义语言环境解析器
public class MyLocaleResolver implements LocaleResolver {
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        //获取请求的语言
        String language = request.getParameter("l");
        //System.out.println("language====>"+language);
        //创建默认的地区
        Locale locale = Locale.getDefault();
        //如果url中有国际化的参数
        if (!StringUtils.isEmpty(language)){
            //zh_CN 国家 _ 地区
            String[] split = language.split("_");
            locale = new Locale(split[0],split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {

    }
}
